Javascript - pass by sharing


如果用.push等操作,會影響原始值;如果是直接賦予新值,不會影響原始值
1.

function modifyArray(arr) {
  arr.push(4)
}

let myArray = [1, 2, 3]
modifyArray(myArray)
console.log(myArray) // [1, 2, 3, 4]

2.

function modifyArray(arr) {
  arr = [1, 2, 3, 4]
}

let myArray = [1, 2, 3]
modifyArray(myArray)
console.log(myArray) // [1, 2, 3]






你可能感興趣的文章

每日心得筆記 2020-07-06(一)

每日心得筆記 2020-07-06(一)

Kotlin 讀書會

Kotlin 讀書會

DAY5:Delete occurrences of an element if it occurs more than n times

DAY5:Delete occurrences of an element if it occurs more than n times






留言討論